package com.mcflies.ui.handler;

import com.mcflies.commons.McfliesConstant;
import com.mcflies.ui.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HandlerMainActivity extends Activity implements OnClickListener {
	private Button handlerMain_Btn_start, handlerMain_Btn_Stop;

	// Handler mainHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.android_handler_main);
		// 例一
		handlerMain_Btn_start = (Button) findViewById(R.id.handlerMain_Btn_start);
		handlerMain_Btn_Stop = (Button) findViewById(R.id.handlerMain_Btn_Stop);
		handlerMain_Btn_start.setOnClickListener(this);
		handlerMain_Btn_Stop.setOnClickListener(this);

		// 例二
		// mainHandler.post(mainRunnable);
		// System.out
		// .println("Activity-------->" + Thread.currentThread().getId());
		// System.out.println("Activityname----->"
		// + Thread.currentThread().getName());

		// 例三
		System.out.println("activity----->" + Thread.currentThread().getId());
		// 由android系统内核提供，其中有一个looper专门用于处理消息队列
		HandlerThread handlerThread = new HandlerThread("handler_thread");
		// 在使用getLooper之前，必须先调用start，不然looper是null
		handlerThread.start();

		MyHandler myHandler = new MyHandler(handlerThread.getLooper());
		Message message = myHandler.obtainMessage();
		Bundle b = new Bundle();
		b.putInt("age", 24);
		b.putString("name", "mcfly");
		message.setData(b);
		message.sendToTarget();
	}

	class MyHandler extends Handler {
		public MyHandler() {
		}

		public MyHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle bundle = msg.getData();
			int age = bundle.getInt("age");
			String name = bundle.getString("name");
			System.out.println("age is " + age + ", name is " + name);
			System.out
					.println("handler----->" + Thread.currentThread().getId());
		}
	}

	// Runnable mainRunnable = new Runnable() {
	// @Override
	// public void run() {
	// System.out.println("handler-------->"
	// + Thread.currentThread().getId());
	// System.out.println("hanldername----->"
	// + Thread.currentThread().getName());
	// try {
	// Thread.sleep(5000);
	// } catch (Exception e) {
	//
	// }
	// }
	// };

	Handler handler = new Handler();

	Runnable updateThread = new Runnable() {
		@Override
		public void run() {
			Log.i(McfliesConstant.MCFLIES_LOGINFO_FLAG, "updateThread**"
					+ System.currentTimeMillis());
			// 调用Handler的postDelayed()方法
			// 这个方法的作用是：将要执行的线程对象放入到队列当中，待时间结束后，运行制定的线程对象
			// 第一个参数是Runnable类型：将要执行的线程对象
			// 第二个参数是long类型：延迟的时间，以毫秒为单位
			handler.postDelayed(updateThread, 3000);
		}
	};

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.handlerMain_Btn_start:
			// 调用Handler的post()方法，将要执行的线程对象放到队列当中
			handler.post(updateThread);
			break;
		case R.id.handlerMain_Btn_Stop:
			// 调用Handler的removeCallbacks()方法，删除队列当中未执行的线程对象
			handler.removeCallbacks(updateThread);
			break;
		}
	}
}
